package research.ui.editors;

import java.util.Calendar;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;

import research.domain.Result;
import research.entity.EntityType;
import research.ui.editors.input.EntityEditorInput;

public class ResultEditor extends FormEntityEditor {
	public static String ID = "research.ui.editors.ResultEditor";
	private Text number;
	private DateTime date;
	private Text works_list;
	private Text results_list;
	private Text company_name;
	private Text customer;
	private Text researcher;
	
	private Result getEntity() {
		return (Result) entity;
	}

	@Override
	protected boolean checkInputEntityType(EntityEditorInput input) {
		return input.getEntity().getType().equals(EntityType.Result);
	}

	@Override
	protected void fill() {
		this.works_list.setText(this.getEntity().getWorks_list());
		this.results_list.setText(this.getEntity().getResults_list());
		this.company_name.setText(this.getEntity().getCompany_name());
		this.customer.setText(this.getEntity().getCustomer());
		this.researcher.setText(this.getEntity().getResearcher());
		this.number.setText(this.getEntity().getNumber());

		Calendar newdate = Calendar.getInstance();
		newdate.setTime(this.getEntity().getDate());
		int year = newdate.get(Calendar.YEAR);
		int month = newdate.get(Calendar.MONTH);
		int day = newdate.get(Calendar.DAY_OF_MONTH);
		date.setYear(year);
		date.setMonth(month);
		date.setDay(day);
	}

	@Override
	protected void initialize() {
	}

	@Override
	protected boolean readAndValidate() {
		this.getEntity().setCompany_name(this.company_name.getText());
		this.getEntity().setCustomer(this.customer.getText());
		this.getEntity().setResults_list(this.results_list.getText());
		this.getEntity().setNumber(this.number.getText());
		this.getEntity().setResearcher(this.researcher.getText());
		this.getEntity().setWorks_list(this.works_list.getText());

		Calendar date = Calendar.getInstance();
		date.set(this.date.getYear(), this.date.getMonth(), this.date.getDay(),
				0, 0, 0);
		this.getEntity().setDate(date.getTime());
		
		return validateText(number) && validateText(results_list);
	}

	@Override
	protected void renderForm() {
		TableWrapData td;
		Label label;
		Section section;
		Composite composite;
		GridData gd;
		
		/*
		 * Registration number
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("Регистрационный номер*");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		number = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		number.setLayoutData(gd);
		number.addModifyListener(getTextModifyListener());
		
		/*
		 * Date
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("Дата оформления");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(2, false));
		section.setClient(composite);
		gd = new GridData();
		label = toolkit.createLabel(composite, "Дата*:");
		label.setLayoutData(gd);
		gd = new GridData();
		date = new DateTime(composite, SWT.DATE);
		date.setLayoutData(gd);

		/*
		 * Works
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL_GRAB);
		section.setLayoutData(td);
		section.setText("Список проделанных работ");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		works_list = toolkit.createText(composite, "",
				SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		gd.widthHint = 200;
		gd.heightHint = 60;
		works_list.setLayoutData(gd);
		works_list.addModifyListener(getTextModifyListener());
		
		/*
		 * Results
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL_GRAB);
		section.setLayoutData(td);
		section.setText("Список полученных результатов*");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		results_list = toolkit.createText(composite, "",
				SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		gd.widthHint = 200;
		gd.heightHint = 60;
		results_list.setLayoutData(gd);
		results_list.addModifyListener(getTextModifyListener());
		
		/*
		 * Company
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("Компания-заказчик");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		company_name = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		company_name.setLayoutData(gd);
		company_name.addModifyListener(getTextModifyListener());
		
		/*
		 * Customer
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("ФИО заказчика");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		customer = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		customer.setLayoutData(gd);
		customer.addModifyListener(getTextModifyListener());
		
		/*
		 * Researcher
		 */
		section = toolkit.createSection(form.getBody(), Section.TITLE_BAR);
		td = new TableWrapData(TableWrapData.FILL);
		section.setLayoutData(td);
		section.setText("ФИО ответственного лица");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(1, false));
		section.setClient(composite);
		gd = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
		researcher = toolkit.createText(composite, "",
				SWT.BORDER);
		gd.widthHint = 100;
		researcher.setLayoutData(gd);
		researcher.addModifyListener(getTextModifyListener());

	}

}
